package string;

/**
 * return char 格式: String.charAt(int index)
 * 返回指定位置上的字符
 */
public class ChatAtDemo {
    public static void main(String[] args) {
        String str = "www.tedu.cn";
        char c = str.charAt(5);
        System.out.println(c);

        for (int i = 0; i < str.length(); i++) {
            char s = str.charAt(i);
            System.out.print(s);
        }
        System.out.println(" ");

        //练习: 判断回文
        String line = "121";
        //上海自来水来自海上
        //方法一: 用boolean+for
        boolean flag = true;
        for (int i = 0; i < line.length() / 2; i++) {
            int j = line.length() - 1 - i;
            if (line.charAt(i) != line.charAt(j)) {
                flag = false;
                break;
            }
        }
        if (flag) {
            System.out.println("是回文数");
        } else {
            System.out.println("不是回文数");
        }
        //方法二: 用int
        int i = 0;
        for (; i < line.length() / 2; i++) {
            char c1 = line.charAt(i);
            char c2 = line.charAt(line.length() - 1 - i);
            if (c1 != c2) {
                System.out.println("不是回文数");
                break;
            }
        }
        if (i == line.length() / 2) {
            System.out.println("是回文数");
        }
        //方法三:弄成一个方法调用
        boolean flag1 = getFlag(line);
        if (flag1) {
            System.out.println("是回文数");
        } else {
            System.out.println("不是回文数");
        }
        //方法四:
        int j = 0;
        for (; j < line.length() / 2; j++) {
            char c1 = line.charAt(j);
            char c2 = line.charAt(line.length() - 1 - j);
            if (c1 != c2) {
                System.out.print("不");
                break;
            }
        }
        System.out.println("是回文数");
    }

    //方法三
    public static boolean getFlag(String line) {
        boolean flag = true;
        for (int i = 0; i < line.length() / 2; i++) {
            int j = line.length() - 1 - i;
            if (line.charAt(i) != line.charAt(j)) {
                flag = false;
                return flag;
            }
        }
        return flag;
    }
}
